package com.cmes.mes.basic.core.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cmes.cache.service.CacheService;
import com.cmes.core.constant.enums.EnableStatus;
import com.cmes.jdbc.service.GenericService;
import com.cmes.mes.basic.core.entity.MaterialCategoryEntity;
import com.cmes.mes.basic.core.repository.MaterialCategoryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 物料类型
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class MaterialCategoryService extends GenericService<MaterialCategoryRepository, MaterialCategoryEntity> {

    private static final String CACHED_KEY = "basic:material:category";
    private final CacheService cacheService;

    @SuppressWarnings("unused")
    @PostConstruct
    public void init() {
        cacheService.register(CACHED_KEY, "全部物料类型");
    }

    public List<MaterialCategoryEntity> active() {
        return cacheService.getCache(CACHED_KEY, () -> list(Wrappers.<MaterialCategoryEntity>lambdaQuery()
                .eq(MaterialCategoryEntity::getStatus, EnableStatus.ENABLE.getCode())));
    }

}
